package com.zhoug.android.common.utils;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Window;
import android.view.WindowManager;

/**屏幕方向切换工具
 * 1.注意要在manifest中配置:android:configChanges="orientation|screenSize"
 * 2.在{@link Activity#onConfigurationChanged(Configuration)}方法中调用{@link ScreenOrientationHelper#onConfigurationChanged(Configuration)}
 *  @Author 35574
 * @Date 2020/9/7
 * @Description
 */
public class ScreenOrientationHelper {
    private static final String TAG = ">>>ScreenOrientationHelper";
    private static final boolean DEBUG=true;
    //横屏
    public static final int ORIENTATION_LANDSCAPE=Configuration.ORIENTATION_LANDSCAPE;
    //竖屏
    public static final int ORIENTATION_PORTRAIT=Configuration.ORIENTATION_PORTRAIT;
    //未知
    public static final int ORIENTATION_UNDEFINED=Configuration.ORIENTATION_UNDEFINED;
    private Activity activity;
    private boolean autoFullScreen=true;//true:横屏时设置全屏,隐藏状态栏.竖屏时显示状态栏



    /**
     * 横竖屏改变监听
     */
    private OnScreenOrientationListener OnScreenOrientationListener;

    public ScreenOrientationHelper(Activity activity) {
        this.activity = activity;
    }

    /**
     * 横竖屏切换方法
     */
    public void changeScreenOrientation() {
        if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            if (DEBUG) {
                LogUtils.d(TAG, "changeScreenOrientation:切换成竖屏");
            }
        } else if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            if (DEBUG) {
                LogUtils.d(TAG, "changeScreenOrientation:切换成横屏");
            }
        }
    }

    /**
     * 设置全屏模式:状态栏隐藏和显示
     * @param fullScreen
     */
    private void setFullScreen(boolean fullScreen) {
        Window window = activity.getWindow();
        if(window!=null){
            if (fullScreen) {
                if (DEBUG) {
                    LogUtils.d(TAG, "setFullScreen:设置全屏显示 ");
                }
                WindowManager.LayoutParams attrs = window.getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                window.setAttributes(attrs);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            } else {
                if (DEBUG) {
                    LogUtils.d(TAG, "setFullScreen:取消全屏显示 ");
                }
                WindowManager.LayoutParams attrs = window.getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                window.setAttributes(attrs);
            }
        }
    }

    /**
     * 在{@link Activity#onConfigurationChanged(Configuration)}方法中调用
     * 注意要在manifest中配置:android:configChanges="orientation|screenSize"
     * @param newConfig
     */
    public void onConfigurationChanged(Configuration newConfig){
       if(OnScreenOrientationListener!=null){
           OnScreenOrientationListener.onScreenOrientationChanged(newConfig.orientation);
       }
       if(autoFullScreen){
           setFullScreen(newConfig.orientation == ORIENTATION_LANDSCAPE);
       }
    }

    /**
     * true:横屏时设置全屏,隐藏状态栏;竖屏时显示状态栏
     * @param autoFullScreen 默认值:true
     */
    public void setAutoFullScreen(boolean autoFullScreen) {
        this.autoFullScreen = autoFullScreen;
    }

    /**
     * 横竖屏改变监听
     * @param onScreenOrientationListener
     */
    public void setOnScreenOrientationListener(ScreenOrientationHelper.OnScreenOrientationListener onScreenOrientationListener) {
        OnScreenOrientationListener = onScreenOrientationListener;
    }

    /**
     * 是橫屏
     * @return
     */
    public boolean isLandscape(){
        return activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    }

    /**
     * 是竖屏
     * @return
     */
    public boolean isPortrait(){
        return activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }


    /**
     * 横竖屏改变监听器
     */
    public interface OnScreenOrientationListener{
        /**
         * 横竖屏改变
         * @param screenOrientation {@link #ORIENTATION_PORTRAIT,#ORIENTATION_LANDSCAPE}
         */
        void onScreenOrientationChanged(int screenOrientation);
    }

}
